require(["common", "jQuery", "coAjax", "queryString"], function(jSouper, $, coAjax, QueryString) {

	App.set("$Event.tag.add_new_tag", function() {
		var new_tag_name = $.trim(App.get("$Cache.tag.new_tag_name"));
		if (new_tag_name) {
			coAjax.post(appConfig.bus.add_tag, {
				tag_name: new_tag_name
			}, function(result) {
				var bus_tags_detail = App.get("$Cache.bus_tags_detail") || [];
				bus_tags_detail.unshift(result.result);
				App.set("$Cache.bus_tags_detail", bus_tags_detail);
			});
		}
	});
	// 加载标签列表
	coAjax.get(appConfig.bus.tags_detail, {
		bus_id: busInfo._id
	}, function(result) {
		App.set("$Cache.bus_tags_detail", result.result);
	});
	// 加载默认商品列表，默认是按时间
	coAjax.get(appConfig.goods.list_with_page_info, {
		bus_id: busInfo._id,
		num: 5,
		page: 0
	}, function(result) {
		var info = result.result;
		App.set("$Cache.tile.goods", info.goods);
		page_info = {
			total_num: info.total_num,
			total_page: info.total_page,
			page: info.page,
			num: info.num
		};
		page_info._goods_number_list = [];
		page_info._goods_number_list.length = info.total_page;
		App.set("$Cache.tile.goods_page_info", page_info);
	});

	function _set_editing_tag(data) {
		var detail_content = data && data.detail_content;
		if (detail_content) {
			//默认显示10个
			data.showing_content = detail_content.slice(0, 10);
		}
		App.set("$Cache.tile.editing_tag", data);
	};
	// 点击分类成编辑状态
	App.set("$Event.tile.to_editing", function(e, vm) {
		_set_editing_tag(vm.get());
	});
	//显示更多商品
	App.set("$Event.tile.editing_tag.show_more", function(e, vm) {
		var editing_tag_data = App.get("$Cache.tile.editing_tag");
		if (editing_tag_data) {
			var num = editing_tag_data.showing_content.length;
			//多显示10个
			editing_tag_data.showing_content = editing_tag_data.detail_content.slice(0, num + 10);
			App.set("$Cache.tile.editing_tag", editing_tag_data);
		}
	});
	// 删除标签
	App.set("$Event.tile.remove_tag", function() {
		myConfirm("确定要删除“" + App.get("$Cache.tile.editing_tag.tag_name") + "”这个标签吗？", function() {
			coAjax["delete"](appConfig.bus.remove_tag, {
				tag_id: App.get("$Cache.tile.editing_tag._id")
			}, function(result) {
				App.set("$Cache.bus_tags_detail", result.result);
				_set_editing_tag(null);
			});
		});
	});
	// 删除标签中的商品
	App.set("$Event.tile.remove_goods_from_tag", function(e, vm) {
		coAjax["delete"](appConfig.bus.remove_goods_from_tag, {
			tag_id: App.get("$Cache.tile.editing_tag._id"),
			goods_id: vm.get("_id")
		}, function(result) {
			_set_editing_tag(result.result);
		});
	});
	// 添加标签中的商品
	App.set("$Event.tile.add_goods_to_tag", function(e, vm) {
		coAjax.post(appConfig.bus.add_goods_to_tag, {
			tag_id: App.get("$Cache.tile.editing_tag._id"),
			goods_id: vm.get("_id")
		}, function(result) {
			_set_editing_tag(result.result);
		});
	});
	//进行标签重命名
	App.set("$Event.tile.to_rename_tag", function() {
		App.set("$Cache.tile.is_in_rename_tag", true);
		//缓存标签名
		App.set("$Cache.tile.rename_tag_bak", App.get("$Cache.tile.editing_tag.tag_name"));
	});
	//提交重命名
	App.set("$Event.tile.submit_new_tag_name", function() {
		coAjax.put(appConfig.bus.rename_tag, {
			tag_id: App.get("$Cache.tile.editing_tag._id"),
			tag_name: App.get("$Cache.tile.editing_tag.tag_name")
		}, function(result) {
			_set_editing_tag(result.result);
			App.set("$Cache.tile.is_in_rename_tag", false);
		});
	});
	// 取消重命名
	App.set("$Event.tile.cancel_new_tag_name", function() {
		App.set("$Cache.tile.editing_tag.tag_name", App.get("$Cache.tile.rename_tag_bak"));
		App.set("$Cache.tile.is_in_rename_tag", false);
	});

	var _search_ti;
	App.set("$Event.tile.search", function() {
		clearTimeout(_search_ti);
		_search_ti = setTimeout(function() {
			var search_text = jSouper.trim(App.get("$Cache.tile.search_text"));
			App.set("$Cache.tile.is_searching", !!search_text);
			if (search_text) {
				coAjax.get(appConfig.goods.search, {
					bus_id: busInfo._id,
					q: search_text
				}, function(result) {
					var goods_info_list = result.result;
					var goods_list = [];
					for (var i = 0, item; item = goods_info_list[i]; i += 1) {
						goods_list.push(item.goods);
					}
					App.set("$Cache.tile.search_goods", goods_list);
				});
			}
		}, 120);
	});
});